// TypeScript file
class Main extends eui.UILayer
{
    /**
     * 加载进度界面
     */
    //private loadingView : LoadingUI;

    private isThemeLoadEnd : boolean = false;
    private idLoading : string;
    //private trueLoadingUI : TrueLoadingUI;
    //private homeUI : HomeUI;
    //private loadingBg : egret.Bitmap;

    protected createChildren() : void
    {
        super.createChildren();
        console.log( "theme load ok:", egret.getTimer() );

        //注入自定义素材解析器
        var assetAdapter = new AssetAdapter();
        this.stage.registerImplementation("eui.IAssetAdapter", assetAdapter);
        this.stage.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
        
        //设置加载进度界面
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);
        
        //this.trueLoadingUI = new TrueLoadingUI();
        //this.stage.addChild(this.trueLoadingUI);
        //初始化资源加载库
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");

    }

    private onConfigComplete(event : RES.ResourceEvent) : void 
    {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        //加载皮肤主题配置文件
        var theme = new eui.Theme("resource/default.thm.json", this.stage);
        theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);

        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        
        RES.loadGroup("loading");
    }

    private onThemeLoadComplete(event : eui.UIEvent) : void
    {
        console.log("theme load ok:", egret.getTimer());
        this.isThemeLoadEnd = true;
        this.createScene();

    }

    private onResourceLoadComplete(event : RES.ResourceEvent) : void
    {
        switch (event.groupName)
        {
            case "loading":
            {
                console.log("loading ok:", egret.getTimer());
                if (this.loadingView.parent)
                {
                    this.loadingView.parent.removeChild(this.loadingView);
                }

                Toast.init(this, RES.getRes("toast-bg_png"));
                this.loadingBg = new egret.Bitmap(RES.getRes("loading_bg"));
                this.trueLoadingUI = new TrueLoadingUI();
                this.loadPage("home");
            }
            break;
            case "home":
            {
                console.log("home ok", egret.getTimer());
                this.isResourceLoadEnd = true;
                this.createScene();

            }
            break;
            default:
            {
                console.log("\tpage["+event.groupName + "]ok:", egret.getTimer());
                this.pageLoadedHandler(event.groupName);
            }
            break;
        }

    }

    private onResourceLoadError(event : RES.ResourceEvent) : void
    {

    }

    private onResourceProgress(event : RES.ResourceEvent) : void
    {
        switch (event.groupName)
        {
            case "loading":
                this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
                //this.trueLoadingUI.setProgress(event.itemsLoaded, event.itemsTotal);
                break;
            default:
                this.trueLoadingUI.setProgress(event.itemsLoaded, event.itemsTotal);
                break;
        }

    }

    private onItemLoadError(evt: RES.ResourceEvent): void
    {

    }
    private createScene() : void
    {
        console.log("createScene:", this.isThemeLoadEnd);
        if (this.isThemeLoadEnd)
        {
            this.startCreateScene();
        }

       
    }

    private startCreateScene() : void
    {
        console.log("this.trueLoadingUI.parent:");
        this.pageLoadedHandler("home");
        if (this.loadingBg.parent)
        {
            this.loadingBg.parent.removeChild(this.loadingBg);
        } 
        this.homeUI = new HomeUI();
        
        this.homeUI.addEventListener(GameEvents.EVT_LOAD_PAGE, (evt: egret.Event)=>
        {
            console.log("EVT_LOAD_PAGE:", evt.data);
            this.loadPage(evt.data);
        }, this);
        

        this.addChild(this.homeUI);
    }

    private pageLoadedHandler(pName: string): void
    {
        if (pName != "home")
        {
            this.homeUI.pageReadyHandler(this.idLoading);
        }

        if (this.trueLoadingUI.parent)
        {
            this.trueLoadingUI.parent.removeChild(this.trueLoadingUI);
        }
    }
    private loadPage(pName: string): void
    {
        this.addChild(this.trueLoadingUI);
        this.idLoading = pName;

        switch (pName)
        {
            case "heros":
            case "goods":
                {
                    RES.loadGroup("heros_goods");
                }
                break;
            default:
                {
                    RES.loadGroup(pName);
                }
                break;
        }
    }

    private loadingView: LoadingUI;
    private trueLoadingUI: TrueLoadingUI;
    private loadingBg: egret.Bitmap;
    private isResourceLoadEnd: boolean = false;

    private homeUI:HomeUI;
}